package com.haople.sso.ram.authentication.impl;

import com.haople.sso.core.authentication.TicketAuthorization;
import com.haople.sso.core.vo.Ticket;
import com.haople.sso.ram.util.RAMUtil;

/**
 * 将票据放于RAM 内存中
 * @author Administrator
 *
 */
public class RAMTicketAuthorization implements TicketAuthorization {

	/**
	 * 验证票据
	 */
	public boolean authorization(Ticket ticket) throws Exception {
		if(ticket==null || ticket.getUserId()==null || ticket.getUserId().equals("") || ticket.getTicket()==null || ticket.getTicket().equals("")){
			return false;
		}
		Ticket RAMTicket=null;
		if((RAMTicket=RAMUtil.get(ticket.getUserId()))!=null && RAMTicket.getTicket().equals(ticket.getTicket())){
			return true;
		};
		return false;
	}

	/**
	 * 将票据放入内存中
	 */
	public boolean save(Ticket ticket) throws Exception{
		return RAMUtil.put(ticket);
	}

	/**
     * 根据用户ID 获取票据
     * @param userId
     * @return
	 * @throws Exception 
     */
    public Ticket get(String userId) throws Exception{
    	if(userId==null || userId.equals("")){
    		return null;
    	}
    	return RAMUtil.get(userId);
    }

}
